<?php
/**
 * Created by PhpStorm.
 * UserModel: LLW
 * Date: 2020/1/02/0001
 * Time: 15:55
 */
use think\Db;
use think\Request;
use think\Url;
use think\Config;
use think\View as ViewTemplate;
use think\Response;
use think\exception\HttpResponseException;
//获取后台菜单功能
function caidongweb()
{
    $id = session('usid');
    $rule = Db::name('admin')->where(['cd_admin.ad_id'=>$id,'g.status'=>1])->join('auth_group g','g_id=g.id')->find();
    if($rule){
        if($rule['title'] != '超级管理员'){
            $wheres['id'] = array('in',$rule['rules']);
            $wheres['status'] = 1;
        }else{
            $wheres = array();
        }
        $path = Db::name('auth_rule')->where('pid',0)->where($wheres)->order('condition asc')->select();
        if($path){
            foreach($path as $k=>$v){
                $ruleList = Db::name('auth_rule')->where('pid',$v['id'])->where($wheres)->order('id asc')->select();
                $path[$k]['list'] = $ruleList;
            }
        }else{
            error('没有任何权限','Login/login');
        }
    }else{
        error('没有任何权限','Login/login');
    }
    return $path;
}

function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
    if (is_null($url)) {
        $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
    } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
        $url = Url::build($url);
    }

    $type = getResponseType();
    $result = [
        'code' => 0,
        'msg'  => $msg,
        'data' => $data,
        'url'  => $url,
        'wait' => $wait,
    ];

    if ('html' == strtolower($type)) {
        $template = Config::get('template');
        $view = Config::get('view_replace_str');
        $result = ViewTemplate::instance($template, $view)
            ->fetch(Config::get('dispatch_error_tmpl'), $result);
    }

    $response = Response::create($result, $type)->header($header);

    throw new HttpResponseException($response);
}
function getResponseType()
{
    return Request::instance()->isAjax()
        ? Config::get('default_ajax_return')
        : Config::get('default_return_type');
}

/**
 * 生成随机文章编号
 * @return int
 */
function articleNumber()
{
    $code = rand(1000000,9999999);
    $find = Db::name('article')->where('ar_sn',$code)->field('ar_id')->find();
    if(empty($find)){
        return $code;
    }
    articleNumber();
}

/**
 * 获取表的总数据量
 * @param $table 表名
 * @param string $where 条件
 * @return int|string
 */
function tableCount($table,$where = '')
{
    $count = Db::name($table)->where($where)->count();
    return $count;
}
/**
 * 查询指定数据
 * @param $table
 * @param $where
 * @return object
 */
function find($table,$where)
{
    if($table == '' || $where == ''){
        return false;
    }
    $res = Db::name($table)->where($where)->find();
    return $res;
}

/**
 * 快递公司
 * @return array
 */
function courier()
{
    //快递编码
    $arr =  array(
    '1' => array('圆通快递','yuantong'),
    '2' => array('中通速递','zhongtong'),
    '3' => array('申通快递','shentong'),
    '4' => array('百世快递','huitongkuaidi'),
    '5' => array('全峰快递','quanfengkuaidi'),
    '6' => array('韵达快递','yunda'),
    '7' => array('顺丰速递','shunfeng'),
    '8' => array('天天快递','tiantian'),
    '9' => array('邮政EMS','ems'),
    '10'=> array('德邦物流','debangwuliu')
    );
    return $arr;
}